﻿#define _CRT_SECURE_NO_WARNINGS 1

#include "snake.h"

//设坐标
void SetPos(short x, short y)////////short
{
	COORD pos = { x, y };
	HANDLE hOutput = NULL;
	//获取标准输出的句柄(⽤来标识不同设备的数值)
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	//设置标准输出上光标的位置为pos
	SetConsoleCursorPosition(hOutput, pos);
}

//欢迎界面
void WelcomeToGame(void)
{
	SetPos(40, 15);
	printf("欢迎来到贪吃蛇小游戏");
	SetPos(40, 25);//让按任意键的光标好看
	system("pause");
	system("cls");////////
	SetPos(25,12);
	printf("用↑↓←→分别控制蛇的移动，F3为加速，F4为减速");
	SetPos(25, 13);
	printf("加速得分更高");
	SetPos(40,25);
	system("pause");
	system("cls");
}

void CreatMap(void)
{
	SetPos(0,0);
	for (int i = 0; i < 58; i += 2)//
	{
		//不需要SetPos(0, i);
		wprintf(L"%c", wall);
	}
	SetPos(0, 26);
	for (int j = 0; j < 58; j += 2)
	{
		wprintf(L"%c", wall);
	}
	for (int k = 1; k < 26;k++)
	{
		SetPos(0,k);
		wprintf(L"%c", wall);
	}
	for (int q = 1; q < 26; q++)
	{
		SetPos(56, q);
		wprintf(L"%c", wall);
	}
}

void InitSnake(pSnake ps)
{
	pSnakeNode cur = NULL;
	int i = 0;
	for (i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InitSnake:malloc");
			return;
		}
		cur->x = POS_X + 2 * i;//最后一个是头部，所以用头插
		cur->y = POS_Y;
		cur->next = NULL;
		if (ps->_pSnake)
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
		else
		{
			ps->_pSnake = cur; 
		}
		cur = ps->_pSnake;
		Printf(cur);
		ps->_Score = 0;
		ps->_foodWeight = 10;
		ps->_SleepTime = 200;
		ps->_Dir = RIGHT;//状态，方向也要设置
		ps->_Status = OK;
	}
}



void Printf(pSnakeNode cur)
{
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", body);
		cur = cur->next;
	}
}



void CreatFood(pSnake ps)
{
	int x = 0;
	int y = 0;
	again:
		do
		{
			x = rand() % 53 + 2;
			y = rand() % 25 + 1;
		} while (x % 2);
		pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		if (x == cur->x && y == cur->y)
		{
			goto again;//和上一次结果相同概率较小，不考虑
		}
		cur = cur->next;
	}
	//ps->_pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	//if (ps->_pFood == NULL)
	//{
	//	perror("CreatFood:malloc");
	//	return;
	//}
	//ps->_pFood->x = x;@##￥￥%%%……对NULL引用
	//ps->_pFood->y = y;
	//ps->_pFood->next = NULL;//可以没有，反正用不到
	//SetPos(ps->_pFood->x, ps->_pFood->y);
	//wprintf(L"%c", food);

	//得再创一个，否则 存在对NULL的引用
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pFood == NULL)
	{
		perror("CreatFood:malloc");
		return;
	}
	pFood->x = x;
	pFood->y = y;
	SetPos(pFood->x, pFood->y);
	wprintf(L"%c", food);
	ps->_pFood = pFood;
}

void GameStart(pSnake ps)
{
	system("mode con cols=100 lines=30");////
	system("title 贪吃蛇");
	//获取句柄
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(hOutput, &CursorInfo);//获取控制台光标信息
	CursorInfo.bVisible = false; //隐藏控制台光标
	SetConsoleCursorInfo(hOutput, &CursorInfo); //设置
	WelcomeToGame();
	CreatMap();
	InitSnake(ps);
	CreatFood(ps);


}

int KillByWall(pSnake ps)
{
	if ((ps->_pSnake->x == 0)
		|| (ps->_pSnake->x == 56)
		|| (ps->_pSnake->y == 0)
		|| (ps->_pSnake->y == 26))
	{
		ps->_Status = KILL_BY_WALL;
		return 1;
	}
	return 0;
}

int KillBySelf(pSnake ps)//返回int
{
	pSnakeNode cur = ps->_pSnake->next;
	while (cur)
	{
		if ((ps->_pSnake->x == cur->x)
			&& (ps->_pSnake->y == cur->y))
		{
			ps->_Status = KILL_BY_SELF;
			return 1;
		}
		cur = cur->next;
	}

void PrintHelpInfo(void)
{
	SetPos(64, 15);
	printf("不能穿墙，不能咬到自己\n");
	SetPos(64, 16);
	printf("用↑↓←→分别控制蛇的移动\n");
	SetPos(64, 17);
	printf("F3为加速，F4为减速\n");
	SetPos(64, 18);
	printf("ESC:退出游戏.space:暂停游戏.");
	
}
void pause(void)
{
	while (KEY_PRESS(VK_SPACE) != 0)
	{
		Sleep(300);////////
	}
}

void SnakeMove(pSnake ps) 
{
	pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNextNode == NULL)
	{
		perror("SnakeMove()::malloc()");
		return;
	}
	switch (ps->_Dir)
	{
		case UP:
		{
			pNextNode->x = ps->_pSnake->x;
			pNextNode->y = ps->_pSnake->y - 1;
			break;//
		}
		
		case DOWN:
		{
			pNextNode->x = ps->_pSnake->x;
			pNextNode->y = ps->_pSnake->y + 1;
			break;
		}
		
		case LEFT:
		{
			pNextNode->x = ps->_pSnake->x - 2;
			pNextNode->y = ps->_pSnake->y;
			break;
		}
		case RIGHT:
		{
			pNextNode->x = ps->_pSnake->x + 2;
			pNextNode->y = ps->_pSnake->y;
			break;
		}
		
	}
	if((pNextNode->x == ps->_pFood->x) && (pNextNode->y == ps->_pFood->y))
	{
		EatFood(pNextNode,ps);
	}
	else
	{
		NoFood(pNextNode,ps);
	}
	KillByWall(ps);
	KillBySelf(ps);//在这判断一下是为了改变状态，为GameEnd做准备
}

void EatFood(pSnakeNode psn, pSnake ps)
{
	psn->next = ps->_pSnake;
	ps->_pSnake = psn;
	pSnakeNode cur = ps->_pSnake;
	Printf(cur);
	ps->_Score += ps->_foodWeight;
	free(ps->_pFood);//销毁食物并创建
	CreatFood(ps);
}

void NoFood(pSnakeNode psn, pSnake ps)
{

	psn->next = ps->_pSnake;
	ps->_pSnake = psn;
	pSnakeNode cur = ps->_pSnake;
	while (cur->next)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", body);
		cur = cur->next;
	}
	SetPos(cur->x, cur->y);
	printf(" ");//清理哦
	free(cur->next);
	cur->next = NULL;
}

void GameRun(pSnake ps)
{
	PrintHelpInfo();
	do
	{
		SetPos(64, 10);
		printf("得分：%d", ps->_Score);
		printf("每个食物得分：%d分，ps->foodWeight");
		if (KEY_PRESS(VK_UP) && ps->_Dir != DOWN)
		{
			ps->_Dir = UP;/////没按上，但原来是上也可呀
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_Dir != RIGHT)
		{
			ps->_Dir = LEFT;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_Dir != UP)
		{
			ps->_Dir = DOWN;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_Dir != LEFT)
		{
			ps->_Dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->_Status = END_NOMAL;
			break;//注意
		}
		else if (KEY_PRESS(VK_F3))
		{
			if (ps->_SleepTime >= 50)
			{
				ps->_SleepTime - +30;
				ps->_foodWeight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			if (ps->_SleepTime < 350)
			{
				ps->_SleepTime += 30;
				ps->_foodWeight -= 2;
				if (ps->_SleepTime == 350)//防止为负数
				{
					ps->_foodWeight = 1;
				}
			}
		}
		Sleep(ps->_SleepTime);
		SnakeMove(ps);
	}while (ps->_Status == OK);
	
}
//
void GameEnd(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake;
	SetPos(24, 12);
	switch (ps->_Status)
	{
		case END_NOMAL:
			printf("您主动退出游戏\n");
			break;
		case KILL_BY_SELF:
			printf("您撞上⾃⼰了 ,游戏结束!\n");
			break;
		case KILL_BY_WALL:
			printf("您撞墙了,游戏结束!\n");
			break;
	}
	while (cur)
	{
		ps->_pSnake = cur->next;
		free(cur);
		cur = ps->_pSnake;
	}
}
